<template>
  <div class="role-container">
    <!-- 左侧 侧边tab栏-->

    <div class="left-wrapper">
      <!--
          tab切换
              思路：tab切换类交互的实现通用思路
              1. 点击谁就把谁的index值记录下来(需要一个独一无二的标识就可以 id)
              2. 通过动态控制class类名 active  :class="{active: index === 当前项的下标值}"
       -->
      <div
        v-for="(item,index) in RoleList"
        :key="item.roleId"
        class="role-item"
        :class="{active:currentIndex === index}"
        @click="switchTab(index)"
      >
        <div class="role-info">
          <!-- svg -->
          <svg-icon :icon-class="currentIndex === index? 'user-active':'user'" class="icon" />
          {{ item.roleName }}
        </div>
        <div class="more">
          <!-- 准备下拉框 =下拉菜单 -->
          <el-dropdown>
            <!-- span 行内文字标签 -->
            <span class="el-dropdown-link">
              <!-- svg ... -->
              <svg-icon icon-class="more" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <!-- 跳转编辑携带roleId -->
              <el-dropdown-item @click.native="$router.push(`/addRole?id=${item.roleId}`)">编辑角色</el-dropdown-item>
              <el-dropdown-item @click.native="delRole(item.roleId)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/addRole')">添加角色</el-button>
    </div>

    <!-- 右侧  树状图-->
    <div class="right-wrapper">
      <!-- Tabs 标签页 -->
      <el-tabs v-model="activeName">
        <!--first  -->
        <el-tab-pane label="功能权限" name="first">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title"> {{ item.title }} </div>
              <!-- 树组件 -->
              <el-tree
                ref="tree"
                :data="item.children"
                :props="defaultProps"
                :show-checkbox="true"
                highlight-current=""
                node-key="id"
                default-expand-all
              />
            </div>
          </div>
        </el-tab-pane>
        <!--second  -->
        <el-tab-pane label="成员列表" name="second">
          <div class="user-wrapper">
            <el-table
              :data="roleUserList"
              style="width: 100%"
            >
              <el-table-column
                type="index"
                width="250"
                label="序号"
              />
              <el-table-column
                prop="name"
                label="员工姓名"
              />
              <el-table-column
                prop="userName"
                label="登录账号"
              />
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>

  </div>
</template>

<script>
import { getRoleListAPI, getTreeListAPI, getRoleDetailAPI, getRoleUserAPI, delRoleUserAPI } from '@/api/system'

function addDisabled (treeList) {
  treeList.forEach(item => {
    item.disabled = true
    if (item.children) {
      addDisabled(item.children)
    }
  })
}

export default {
  name: 'Role',
  data() {
    return {
      RoleList: [], // 角色列表
      currentIndex: 0, // 记录的索引
      treeList: [], // 权限树形列表
      defaultProps: {
        children: 'children', // 读取子节点的字段名
        label: 'title', // 显示字段的名字
        disabled: () => true
      },
      perms: [], // 当前角色权限点列表
      activeName: 'first',
      roleUserList: [] // 成员列表

    }
  },

  // 生命周期里先以第一项roleId获取
  async mounted() {
    // 先获取角色列表和可选权限列表
    await this.getRoleList()
    await this.getTreeList()
    // 再获取当前角色下的权限列表
    // 组件初始化的时候模拟点击一下第一项
    // 1. this.roleList不能是空 2.treeList也不能为空  意味着需要等到上面的俩个方法中的异步请求返回之后才能拿调用
    this.switchTab(0)
  },
  methods: {

    // 删除
    delRole(roleId) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 删除接口
        await delRoleUserAPI(roleId)
        // 列表接口
        this.getRoleList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      })
    },

    // 高亮成员
    // 1. 使用id获取高亮权限点列表
    async heightLightPerms(roleId) {
      const res = await getRoleDetailAPI(roleId)
      console.log(res)
      const perms = res.data.perms
      console.log(this.$refs.tree)
      // 3. 遍历tree实例组成的数组 分别调用它身上的高亮方法 传入需要高亮的权限点数据
      this.$refs.tree.forEach((treeInstance, index) => {
        treeInstance.setCheckedKeys(perms[index])
      })
    },
    // 角色列表
    async getRoleList() {
      const resMemeber = await getRoleListAPI()
      this.RoleList = resMemeber.data
    },
    // 成员列表
    async getUserMember(roleId) {
      const res = await getRoleUserAPI(roleId)
      this.roleUserList = res.data
    },
    // 选择tab
    async switchTab(index) {
      this.currentIndex = index
      // 1. 拿到当前的角色id
      const roleId = this.RoleList[index].roleId
      console.log(roleId)
      this.heightLightPerms(roleId)
      this.getRoleList(roleId)
    },
    // 渲染权限配置初始化Tree列表
    async getTreeList() {
      const res = await getTreeListAPI()
      // 直接赋值 没有disabled为true
      // 思路: 针对于treeList中的每一项（每一层中的每一项）都动态添加一个disabled为true
      this.treeList = res.data
      addDisabled(this.treeList)
    }

  }

}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding:10px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn{
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper{
      padding:20px;
    }
  }
}
</style>
